package com.liberty.dasheen.admin.daily.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liberty.dasheen.admin.daily.service.DailyOrderService;
import com.liberty.dasheen.common.BaseController;
import com.liberty.dasheen.common.util.PageModel;
import com.liberty.dasheen.common.util.SearchFilter;
import com.liberty.dasheen.entity.DailyOrder;
import com.liberty.dasheen.vo.DailyOrderVo;

@Controller
@RequestMapping("/admin/daily/order")
public class OrderController extends BaseController {

	@Autowired
	private DailyOrderService dailyOrderService;

	@RequestMapping("/main")
	public String dailyOrderPage() {
		return "admin/daily/order";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Map<String, Object> addOrder(@RequestBody String json, HttpSession session) {
		try {
			if (json == null || "".equals(json)) {
				return null;
			}
			DailyOrder order = super.jsonToObject(json, DailyOrder.class);
			order = dailyOrderService.addOrder(order, session);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("obj", order);
			map.put("msg", "success");
			return map;
		} catch (Exception e) {
			System.out.println("OrderController | addOrder | Exception:");
			e.printStackTrace();
			return null;
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> updateOrder(@RequestBody String json, HttpSession session) {
		try {
			if (json == null || "".equals(json)) {
				return null;
			}
			DailyOrder order = super.jsonToObject(json, DailyOrder.class);
			order = dailyOrderService.updateOrder(order, session);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("obj", order);
			map.put("msg", "success");
			return map;
		} catch (Exception e) {
			System.out.println("OrderController | updateOrder | Exception:");
			e.printStackTrace();
			return null;
		}
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> deleteOrder(@RequestBody String json, HttpSession session) {
		try {
			if (json == null || "".equals(json)) {
				return null;
			}
			Map<String, Object> m = super.jsonToObject(json, Map.class);
			dailyOrderService.deleteOrder(m.get("ids"), session);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("msg", "success");
			return map;
		} catch (Exception e) {
			System.out.println("OrderController | updateOrder | Exception:");
			e.printStackTrace();
			return null;
		}
	}
	
	@RequestMapping(value = "/page")
	@ResponseBody
	public PageModel<DailyOrderVo> page(@RequestBody String json) {
		try {
			if (json == null || "".equals(json)) {
				return null;
			}
			SearchFilter filter = super.jsonToObject(json, SearchFilter.class);
			List<DailyOrderVo> list = dailyOrderService.list(filter);
			Integer count = dailyOrderService.count(filter);
			return new PageModel<>(list, filter.getPageNum(), filter.getPageSize(), count);
		} catch (Exception e) {
			System.out.println("OrderController | page | Exception:");
			e.printStackTrace();
			return null;
		}
	}
}
